<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const directLine = WebChat.createDirectLine({ token: await testHelpers.token.fetchDirectLineToken() });

        const patchedDirectLine = {
          activity$: new Observable(observer => {
            const subscription = directLine.activity$.subscribe({
              next(activity) {
                observer.next(
                  Object.assign({}, activity, {
                    attachments: (activity.attachments || []).map(attachment =>
                      Object.assign({}, attachment, {
                        contentUrl: 'https://example.org/'
                      })
                    )
                  })
                );
              }
            });

            return () => subscription.unsubscribe();
          }),

          connectionStatus$: directLine.connectionStatus$,
          postActivity: directLine.postActivity.bind(directLine),
          token: directLine.token
        };

        const store = testHelpers.createStore();

        WebChat.renderWebChat(
          {
            directLine: patchedDirectLine,
            store,
            styleOptions: {
              sendAttachmentOn: 'attach'
            }
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        await pageObjects.uploadFile('empty.zip');
        await pageConditions.allOutgoingActivitiesSent();

        await pageConditions.minNumActivitiesShown(2);
        await pageConditions.allImagesLoaded();

        await host.snapshot();

        const [firstActivity, secondActivity] = pageElements.activities();

        expect(firstActivity.querySelector('a[target="_blank"]').getAttribute('href')).toEqual('https://example.org/');
        expect(firstActivity.querySelector('a[target="_blank"]').getAttribute('download')).toEqual('empty.zip');

        expect(secondActivity.querySelector('a[target="_blank"]').getAttribute('href')).toEqual('https://example.org/');
        expect(secondActivity.querySelector('a[target="_blank"]').getAttribute('download')).toEqual('empty.zip');
      });
    </script>
  </body>
</html>
